package com.zjmzxfzhl.modules.purchase.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseMaterial;
import com.zjmzxfzhl.modules.purchase.service.PurchaseMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/material")
public class PurchaseMaterialController extends BaseController {
    @Autowired
    private PurchaseMaterialService purchaseMaterialService;

    /**
     * 自定义查询列表
     *
     * @param purchaseMaterial
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:material:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseMaterial purchaseMaterial, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<PurchaseMaterial> pageList = purchaseMaterialService.list(new Page<PurchaseMaterial>(current, size), purchaseMaterial);
        return Result.ok(pageList);
    }

    /**
     * 查询所有物料
     *
     * @return
     */
    @RequiresPermissions("purchase:material:list")
    @GetMapping(value = "/listAll")
    public Result listAll() {
        List<PurchaseMaterial> materials = purchaseMaterialService.list();
        return Result.ok(materials);
    }

    @RequiresPermissions("purchase:material:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseMaterial purchaseMaterial = purchaseMaterialService.getById(id);
        return Result.ok(purchaseMaterial);
    }

    /**
     * @param purchaseMaterial
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:material:save")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody PurchaseMaterial purchaseMaterial) {
        purchaseMaterialService.save(purchaseMaterial);
        return Result.ok();
    }

    /**
     * @param purchaseMaterial
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:material:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseMaterial purchaseMaterial) {
        purchaseMaterialService.updateById(purchaseMaterial);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:material:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        List<Long> materialIds = Convert.toList(Long.class, idsArr);
        purchaseMaterialService.deleteByIds(materialIds);
        return Result.ok();
    }
}
